<?php
require './model/Db.php';
$isMobile = preg_match('/mobile/i', $_SERVER['HTTP_USER_AGENT']);
if($isMobile){
	$css = [
		'public/css/my-bootstrap.min.css',
		'public/css/module.phone.min.css'
	];
}else{
	$css = [
		'public/css/my-bootstrap.min.css',
		'public/css/module.pc.min.css'
	];
}
$Db = Db::getInstance();
$user_data = $Db->islogin();
$isExist = $Db->find([
	table => [module => 'a',zone => 'b'],
	field => 'a.*,b.id zid,b.name zname',
	where => "a.zid = b.id and a.id = {$_GET['mid']}"
]);

if($isExist){
    $web_title = $isExist[0]['name'];
	include './view/header.html';
	
if(isset($_POST['submit'])){

	if($user_data){
	    $_POST = $Db->escape($_POST);
	    //报修格式处理
	    if($_POST['submit']=='baoxiu'){
	        $address = $_POST['title'];
	        $miaoshu = $_POST['body'];
	
	        $body_h = "✦{$_POST['type']}故障维修申请:\r\n\r\n";
	        $body_m = "\t✧地址：{$_POST['title']}\r\n\t✧申报人：{$_POST['name']}\r\n\t✧联系电话：{$_POST['phone']}\r\n\t✧描述：\r\n{$_POST['body']}";
	        $_POST['title'] = "【{$_POST['type']}报修】 {$_POST['title']}";
	        $_POST['body'] = $body_h.$body_m;
	        
	    }elseif ($_POST['submit']=='shenbao') {
	        $address = $_POST['title'];
	        $body_h = "✦{$_POST['name1']} 活动申请:\r\n\r\n";
	        $body_m = "\t✧活动名称：{$_POST['name1']}\r\n\t✧申报单位：{$_POST['name2']} ({$_POST['type']})\r\n\t✧负责人电话：{$_POST['phone']}\r\n\t✧活动时间：{$_POST['start']}~{$_POST['end']}\r\n\t✧活动场地：{$_POST['title']}\r\n\t✧正文：\r\n{$_POST['body']}";
	        $_POST['title'] = "【活动申请】{$_POST['name2']}申请活动：{$_POST['name1']}";
	        $_POST['body'] = $body_h.$body_m;
	    }
	
		$paths = '';
		if($_POST['files']){
			foreach ($_POST['files'] as $value) {
				$paths .= '"'.$value.'",';
			}
			$paths = trim($paths,',');
		}
		$flag = $Db->save([
			table => 'content',
			map => [
				title => $_POST['title'],
				body => $_POST['body'],
				mid => $isExist[0]['id'],
				file => '{"path":['.$paths.']}',
				uid => $user_data['id'],
			]
		]);
		//查找cid
		    $cont_id = $Db->find([
    			table => ['content'],
    			field => 'id',
    			where => "title='{$_POST['title']}' and body='{$_POST['body']}'"
    		])[0]['id'];
		if($_POST['submit']=='baoxiu'){
    		//存入gzwx表
    		$Db->save([
    			table => 'gzwx',
    			map => [
    				cid => $cont_id,
    				sender => $_POST['name'],
    				sendertell => $_POST['phone'],
    				address => $address,
    				miaoshu => $miaoshu
    			]
    		]);
		}elseif($_POST['submit']=='shenbao'){
    		//存入hdsb表
    		$Db->save([
    			table => 'hdsb',
    			map => [
    				cid => $cont_id,
    				name => $_POST['name1'],
    				sender => $_POST['name2'],
    				sendertell => $_POST['phone'],
    				address => $address,
    				hdtime => $_POST['start'].'~'.$_POST['end']
    			]
    		]);
		}
		
		if($flag){ //加分
			$maxlv = 20+$user_data['job']*5;
			if($user_data['today_lv'] < $maxlv){
				if($user_data['today_lv']+2 > $maxlv){
					$diff = $user_data['today_lv']-$maxlv+2;
					$lv = "#lv+{$diff}#";
					$today_lv = $maxlv;
				}else{
					$lv = '#lv+2#';
					$today_lv = '#today_lv+2#';
				}
				$Db->update([
					table => 'bbs_user',
					map => [
						lv => $lv,
						today_lv => $today_lv
					],
					where => "id = {$user_data['id']}"
				]);
			}
		}
		$msg = $flag ? '发帖成功' : '网络不稳定,请稍后再试~';

	}else{
		$msg = '请先登录,登录后才可以发帖';
	}
	include './view/tip.html';
}
	//版务
	$manager = json_decode($isExist[0]['manager'])->uids;
	$uids = implode(',', $manager);
	if(!$uids){
		$uids = -1;
	}
	$manager_info = $Db->find([
		table => ['bbs_user'],
		field => 'id,user',
		where => "id in ({$uids})"
	]);
	//收藏
	if($user_data){
		$collect_mids = json_decode($user_data['my_follow'])->mids;
		$collect = in_array($_GET['mid'], $collect_mids)?' collected':'';
	}
	/*获取数量*/
	//今日
	$today_cnt = $Db->count([
		table => [content => 'a',module => 'b'],
		where => "a.mid = b.id and b.id = {$_GET['mid']} and a.time > CURDATE()"
	]);
	//主题
	$main_cnt = $Db->count([
		table => [content => 'a',module => 'b'],
		where => "a.mid = b.id and b.id = {$_GET['mid']} and a.cid = 0"
	]);
	//总数
	$all_cnt = $Db->count([
		table => [content => 'a',module => 'b'],
		where => "a.mid = b.id and b.id = {$_GET['mid']}"
	]);
	//获取本地表情
	$bq_arr = [];
	if(is_dir(EXP_FILE_PATH)){
		$dir_handle = opendir(EXP_FILE_PATH);
		while($file = readdir($dir_handle)){//读取dir条目
			if(is_file(EXP_FILE_PATH.$file)){
				$bq_arr[$file] = EXP_FILE_PATH.$file;
			}
		}
	}
}else{
	header('Location:err.php?id=0');
}

$Db->close();

if($isMobile){
	include './view/module.phone.html';
}else{
	include './view/module.pc.html';
}
include './view/footer.html';